<?php

/*
 * To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor.
 */
namespace app\member\controller;

use think\Validate;
use think\Exception;

/**
 * Description of Order
 *
 * @author ayg
 */
class Address extends \app\common\controller\Member
{

    /**
     * 地址列表
     */
    public function index()
    {
        $area_list = $this->db_lists('address', [
            'member_id' => $this->member_id
        ]);
        $this->view->assign('area_list', $area_list);
        return $this->view->fetch();
    }

    /**
     * 添加/编辑地址
     *
     * @return type
     */
    public function modify()
    {
        $address_id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $address_count = rpc('common/common/one', [
                'address',
                [
                    'member_id' => $this->member_id
                ],
                [
                    'field' => 'count(*) as num'
                ]
            ]);
            if ($address_id == 0 && $address_count['num'] > 2) {
                showDialog('最多可保存20个有效地址','reload');
            }
            $rule = [
                "true_name|真实名字" => "max:50",
                "area_id|所在地区" => "number",
                "city_id|所在地区" => "number",
                "area_info|所在地区" => "max:255",
                "address|详细地址" => "max:255",
                "tel_phone|手机号码" => "number",
                "mob_phone|固定电话" => "max:20",
                "if_default|固定电话" => "number"
            ];
            $data = [
                'member_id' => $this->member_id,
                'true_name' => $this->request->param('true_name/s'),
                'area_id' => $this->request->param('area_id/d'),
                'city_id' => $this->request->param('city_id/d'),
                'area_info' => $this->request->param('area_info/s'),
                'address' => $this->request->param('address/s'),
                'tel_phone' => $this->request->param('tel_phone/s'),
                'mob_phone' => $this->request->param('mob_phone/s'),
                'is_default' => $this->request->param('is_default/s', 0)
            ];
            $validate = new Validate($rule);
            if (! $validate->check($data)) {
                // 验证不通过
                showDialog($validate->getError());
            }
            if ($data['is_default']) {
                // 如果当前保存的事默认地址，其他的默认地址都要取消
                rpc('common/common/update', [
                    'address',
                    [
                        'member_id' => $this->member_id
                    ],
                    [
                        'is_default' => 0
                    ]
                ]);
            }
            if ($address_id) {
                // 编辑
                $result = rpc('common/common/update', [
                    'address',
                    [
                        'address_id' => $address_id,
                        'member_id' => $this->member_id
                    ],
                    $data
                ]);
            } else {
                // 添加
                $result = rpc('common/common/create', [
                    'address',
                    $data
                ]);
            }
            if ($result !== false) {
                showDialog('保存成功', 'reload', 'succ');
            } else {
                showDialog('保存地址失败');
            }
        } else {
            $address_info = rpc('common/common/one', [
                'address',
                [
                    'address_id' => $address_id,
                    'member_id' => $this->member_id
                ]
            ]);
            $this->view->assign('address_info', $address_info);
            return $this->view->fetch();
        }
    }

    /**
     * 删除地址
     *
     * @return type
     */
    public function del()
    {
        $address_id = $this->request->param('id/d');
        $result = rpc('common/common/delete', [
            'address',
            [
                'address_id' => $address_id,
                'member_id' => $this->member_id
            ]
        ]);
        if ($result) {
            showDialog('删除成功', 'reload', 'succ');
        } else {
            showDialog('删除失败');
        }
    }
}
